package ooday04;
/** 多态的演示 */
public class PolymorphicDemo {
    public static void main(String[] args) {


        //演示强转成功的条件
        /*
        Animal o = new Dog("小黑",2,"黑");
        Dog g = (Dog)o;
        Swim s = (Swim)o;
        //Fish f = (Fish)o;

        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish);//false
        if (o instanceof Fish){
            Fish fish = (Fish)o;
        }

         */


       //演示向上造型(多态)的第2点应用:
        //Animal animal = new Animal(); 编译错误，抽象类不能被实例化
        Animal[] animals = new Animal[5];
        animals[0] = new Dog("大黄",1,"黄");//向上造型
        animals[1] = new Chick("大花",2,"花");
        animals[2] = new Fish("大金",3,"金");
        animals[3] = new Dog("大红",5,"红");
        animals[4] = new Chick("大紫",6,"紫");
        for (int i=0;i<animals.length;i++){//遍历所有动物
            System.out.println(animals[i].name);//输出每个动物的名字
            animals[i].drink();//每个动物喝水
            animals[i].eat();//每个动物吃饭

            if (animals[i] instanceof Dog){
                Dog dog = (Dog)animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick = (Chick)animals[i];
            }
            if (animals[i] instanceof Fish){
                Fish fish = (Fish)animals[i];
            }
            if (animals[i] instanceof Swim){//适用于所有实现Swim接口的对象（会游泳的）
                Swim s = (Swim)animals[i];
                s.swim();
            }

        }




        /*
        //演示向上转型
        Dog o1 = new Dog("小黑",2,"黑");
        //o1.name/age/color/eat/drink/lookHome swim
        Animal o2 = new Dog("小黑",2,"黑");
        //o2.name/age/color/drink/eat
        Swim o3 = new Dog("黑色",2,"黑");
        //o3.swim();
         */

        /*
        //演示向上造型(多态)的第1点应用:
        Master master = new Master();
        Dog dog = new Dog("'小黑",2,"黑");
        Chick chick = new Chick("小花",3,"花");
        Fish fish = new Fish("小金",1,"金");
        master.feed(dog);//在传参的同时,系统自动做了向上造型
        master.feed(chick);
        master.feed(fish);
         */


    }
}
